//
//  NIMRRightMerchandiseCell.m
//  QianbaoIM
//
//  Created by qianwang on 14/11/18.
//  Copyright (c) 2014年 liu nian. All rights reserved.
//

#import "NIMRRightMerchandiseCell.h"
#import "NSAttributedString+Attributes.h"

@implementation NIMRRightMerchandiseCell

- (void)updateWithRecordEntity:(ChatEntity *)recordEntity{
    [super updateWithRecordEntity:recordEntity];
    
    NSString* sbody = recordEntity.msgContent;
    NSDictionary *htmDic = [NIMUtility jsonWithJsonString:sbody];
    NSString *imageStr = PUGetObjFromDict(@"img_url", htmDic, [NSString class]);
    NSString *titleStr = PUGetObjFromDict(@"title", htmDic, [NSString class]);
    NSString *price = PUGetObjFromDict(@"price", htmDic, [NSString class]);
    NSString *inventory =PUGetObjFromDict(@"desc", htmDic, [NSString class]);
    
    [self.iconView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:IMG2XFROMBUNDLE(@"bg_dialog_pictures")];
    self.titleLabel.text = titleStr;
    if (IsStrEmpty(price) || price.doubleValue ==0)
    {
        self.priceLabel.text=@"面议";
    }
    else
    {
        price = _IM_FormatStr(@"%.0f",price.doubleValue * 100);
//        price = [SSIMSpUtil toThousand:price];
//        price = [SSIMSpUtil removePoint:price];
        price = [SSIMSpUtil toThousand:[SSIMSpUtil convertDoubleToDoubleDigit:fabs(price.doubleValue)/100]];
        NSString *str = _IM_FormatStr(@"价格: "__QB_UNIT__"%@" , price);
        NSString * decimal = [SSIMSpUtil decimalString:str];

        NSMutableAttributedString* attrStr = [NSMutableAttributedString attributedStringWithString:str];
        [attrStr setTextColor:[UIColor lightGrayColor]];
        [attrStr setFont:FONT_TITLE(12.0)];
        [attrStr setFont:FONT_TITLE(11) range:[str rangeOfString:@__QB_UNIT__]];
        [attrStr setFont:FONT_TITLE(11) range:[str rangeOfString:decimal]];

        self.priceLabel.attributedText = attrStr;
    }
    self.inStockLabel.text = [NSString stringWithFormat:@"库存: %@ 件",inventory];
}


- (void)makeConstraints{
    [super makeConstraints];
    
    if (self.showName) {
        [self.paoButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.vNameLabel.mas_bottom).with.offset(5);
            make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
            make.trailing.equalTo(self.avatarBtn.mas_leading).with.offset(-10);
            make.width.equalTo(@(kMarginPaoPaoWidth));
            //            make.height.equalTo(@100);
        }];
        
    }else{
        
        [self.paoButton mas_remakeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.avatarBtn.mas_top);
            make.bottom.equalTo(self.contentView.mas_bottom).with.offset(-10);
            make.trailing.equalTo(self.avatarBtn.mas_leading).with.offset(-10);
            make.width.equalTo(@(kMarginPaoPaoWidth));
            //            make.height.equalTo(@100);
        }];
    }
    
    [self.titleLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.paoButton.mas_top).with.offset(5);
        make.leading.equalTo(self.paoButton.mas_leading).with.offset(10);
        make.trailing.equalTo(self.paoButton.mas_trailing).with.offset(-16);
        make.height.lessThanOrEqualTo(@40);
    }];
    
    [self.iconView mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.titleLabel.mas_bottom).with.offset(5);
        //        make.bottom.equalTo(self.paoButton.mas_bottom).with.offset(-5);
        make.leading.equalTo(self.paoButton.mas_leading).with.offset(10);
        make.width.equalTo(@60);
        make.height.equalTo(@60);
    }];
    
    [self.inStockLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_top);
        make.leading.equalTo(self.iconView.mas_trailing).with.offset(5);
        make.trailing.equalTo(self.paoButton.mas_trailing).with.offset(-18);
        make.height.greaterThanOrEqualTo(@16);
        make.bottom.equalTo(self.iconView.mas_bottom);
    }];
//    [self.priceLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.iconView.mas_top);
//        make.leading.equalTo(self.iconView.mas_trailing).with.offset(5);
//        make.trailing.equalTo(self.paoButton.mas_trailing).with.offset(-18);
//        make.height.greaterThanOrEqualTo(@16);
//    }];
    
//    [self.inStockLabel mas_remakeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.priceLabel.mas_bottom);
//        make.leading.equalTo(self.priceLabel.mas_leading);
//        make.trailing.equalTo(self.paoButton.mas_trailing).with.offset(-18);
//        make.height.greaterThanOrEqualTo(@16);
//    }];
    
}


#pragma mark actions
- (IBAction)tapRecognizerHandler:(UITapGestureRecognizer*)gesture
{
    [_delegate chatTableViewCell:self didSelectedWithRecordEntity:self.recordEntity];
}
- (void)actionForward:(id)sender{
    [_delegate chatTableViewCell:self didSelectedWithRecordEntity:self.recordEntity action:ChatMenuActionTypeForward];
}
#pragma mark getter
- (NSArray *)menuItems{
    if (!_menuItems) {
        _menuItems = @[kMenuItemForward];
    }
    return _menuItems;
}
- (UIImage *)image{
    if (!_image) {
        _image = [IMGFROMBUNDLE(@"bg_dialog_-forward_right")
                  resizableImageWithCapInsets:UIEdgeInsetsMake(30, 15, 10, 30) resizingMode:UIImageResizingModeStretch];
    }
    return _image;
}

- (UIImage *)highlightedImage{
    if (!_highlightedImage) {
        _highlightedImage = [IMG2XFROMBUNDLE(@"bg_dialog_-forward_right_highlight")
                             resizableImageWithCapInsets:UIEdgeInsetsMake(30, 15, 10, 30) resizingMode:UIImageResizingModeStretch];
    }
    return _highlightedImage;
}
- (UIImageView *)iconView{
    if (!_iconView) {
        _iconView = [[UIImageView alloc] initWithImage:IMG2XFROMBUNDLE(@"")];
        _iconView.clipsToBounds = YES;
        _iconView.contentMode = UIViewContentModeScaleAspectFill;
        _iconView.layer.cornerRadius = 2;
        [self.contentView addSubview:_iconView];
    }
    return _iconView;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        _titleLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _titleLabel.font = [UIFont boldSystemFontOfSize:15];
        _titleLabel.textColor = [SSIMSpUtil getColor:@"262626"];
        _titleLabel.numberOfLines = 2;
        _titleLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_titleLabel];
    }
    return _titleLabel;
}
- (UILabel *)priceLabel{
    if (!_priceLabel) {
        _priceLabel = [[UILabel alloc] initWithFrame:CGRectZero];
        _priceLabel.font = [UIFont systemFontOfSize:13];
        _priceLabel.textColor = [SSIMSpUtil getColor:@"888888"];
        _priceLabel.numberOfLines = 4;
        
        _priceLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_priceLabel];
    }
    return _priceLabel;
}
- (NIMMyLable *)inStockLabel{
    if (!_inStockLabel) {
        _inStockLabel = [[NIMMyLable alloc] initWithFrame:CGRectZero];
        _inStockLabel.font = [UIFont systemFontOfSize:13];
        _inStockLabel.textColor = [SSIMSpUtil getColor:@"888888"];
        _inStockLabel.numberOfLines = 0;
        _inStockLabel.verticalAlignment = VerticalAlignmentTop;
        _inStockLabel.textAlignment = NSTextAlignmentLeft;
        [self.contentView addSubview:_inStockLabel];
    }
    return _inStockLabel;
}

@end
